﻿using AdverseEventReportSystem.BLL;
using AdverseEventReportSystem.BLL.ServiceReference1;
using AdverseEventReportSystem.Core;
using AdverseEventReportSystem.Model;
using AdverseEventReportSystem.Utils;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace AdverseEventReportSystem.Web.Areas.Personnel
{
    public class EditPersonnelController : BaseController
    {
        // GET: Personnel/EditPersonnel
        public ActionResult Index(int id = 0, int pageType = 0, string detailsViewBtns = "", int status = 0, params Object[] objects)
        {
            ViewBag.PersonnelId = id;
            ViewBag.PageType = pageType;
            ViewBag.DetailsViewBtns = detailsViewBtns;
            ViewBag.Status = status;
            return View();
        }

        /// <summary>
        /// 获取民族下来框
        /// </summary>
        /// <returns></returns>
        public ActionResult GetNationList()
        {

            Nation nation = new Nation();
            nation = XmlHelper.ReadXML<Nation>(Server.MapPath("~/") + "Xml\\Nation.xml", nation);

            List<ComboBoxItem> itemList = new List<ComboBoxItem>();
            foreach (var item in nation.Items)
            {
                itemList.Add(new ComboBoxItem() { value = item.Text, text = item.Text });
            }

            return JsonDate(itemList);
        }

        /// <summary>
        /// 上传图片
        /// </summary>
        /// <param name="fc"></param>
        /// <returns></returns>
        [LoginAttribute(IsNeed = false)]
        public string Upload(FormCollection fc, string folderType)
        {
            string newFileName = string.Empty;
            string folderPath = "";
            if (DataConvert.StrToInt(folderType) == 0)
            {
                folderPath = "~/Upload/Personnel/Photo/";
            }
            else
            {
                folderPath = "~/Upload/Personnel/File/";
            }
            if (Request.Files.Count != 0)
            {
                //HttpPostedFileBase类，提供对用户上载的单独文件的访问
                //获取到用户上传的文件
                HttpPostedFileBase file = Request.Files[0];

                //获取用户上传文件的后缀名
                string Extension = Path.GetExtension(file.FileName);

                //重新命名文件
                newFileName = Guid.NewGuid().ToString() + Extension;

                //利用file.SaveAs保存图片
                string name = Path.Combine(Server.MapPath(folderPath), newFileName);

                file.SaveAs(name);
            }
            return folderPath.Replace("~", "../..") + newFileName;
        }

        /// <summary>
        /// 删除图片
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public ActionResult DelFile(string path)
        {
            OperateResult resultInfo = PersonnelManager.DelFile(path);
            return JsonDate(resultInfo);
        } 

        /// <summary>
        /// 生成档案编号
        /// </summary>
        /// <returns></returns>
        public ActionResult GetPersonnelNo()
        {
            OperateResult resultInfo = PersonnelManager.GetPersonnelNo();
            return JsonDate(resultInfo);
        }

        /// <summary>
        /// 获取人员档案详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult GetPersonnelInfo(string id)
        {
            OperateResult resultInfo = PersonnelManager.GetPersonnelInfo(id);
            return JsonDate(resultInfo);
        }

        /// <summary>
        /// 获取登录用户在User表的信息
        /// </summary>
        /// <returns></returns>
        public ActionResult GetUser()
        {
            OperateResult resultInfo = UserManager.GetUser(loginUser.Id);
            return JsonDate(resultInfo);
        }

        /// <summary>
        /// 添加/编辑人员档案
        /// </summary>
        /// <param name="personnelModel"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Save(PersonnelModel personnelModel)
        {
            OperateResult resultInfo = PersonnelManager.Save(personnelModel);
            return JsonDate(resultInfo);
        }

        /// <summary>
        /// 添加培训进修
        /// </summary>
        /// <param name="trainingRecord"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult AddTrainingRecord(P_TrainingRecord trainingRecord)
        {
            OperateResult resultInfo = PersonnelManager.AddTrainingRecord(trainingRecord);
            return JsonDate(resultInfo);
        }

        /// <summary>
        /// 添加论文发表
        /// </summary>
        /// <param name="paperRecord"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult AddPaperRecord(P_PaperRecord paperRecord)
        {
            OperateResult resultInfo = PersonnelManager.AddPaperRecord(paperRecord);
            return JsonDate(resultInfo);
        }

        /// <summary>
        /// 添加科研情况
        /// </summary>
        /// <param name="researchRecord"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult AddResearchRecord(P_ResearchRecord researchRecord)
        {
            OperateResult resultInfo = PersonnelManager.AddResearchRecord(researchRecord);
            return JsonDate(resultInfo);
        }

        /// <summary>
        /// 添加奖惩记录
        /// </summary>
        /// <param name="rewardAndPunishRecord"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult AddRewardAndPunishRecord(P_RewardAndPunishRecord rewardAndPunishRecord)
        {
            OperateResult resultInfo = PersonnelManager.AddRewardAndPunishRecord(rewardAndPunishRecord);
            return JsonDate(resultInfo);
        }
    }
}